package com.goldze.mvvmhabit.ui.selectsdepartment;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.databinding.SelectDepartmentBinding;
import com.goldze.mvvmhabit.utils.UserManager;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;

public class SelectDepartmentActivity extends BaseActivity<SelectDepartmentBinding, SelectDepartmentViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.select_department;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SelectDepartmentViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SelectDepartmentViewModel.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.showDialogChangeDepartSuccess.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String value) {
                showDialogChangeDepartSuccess(value);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        //请求网络数据
        viewModel.requestNetWork();
        //初始化标题
        viewModel.initToolbar();

        if (UserManager.getAppManager().isLogin()) {
            if ("1".equals(String.valueOf(UserManager.getAppManager().getmUserInfo().getSex()))) {
                binding.logoHeadIv.setImageResource(R.mipmap.man_icon_s);
            } else {
                binding.logoHeadIv.setImageResource(R.mipmap.woman_icon_s);
            }

        }
    }

    public void showDialogChangeDepartSuccess(final String value) {
        MaterialDialogUtils.showBasicDialog(this, "提示", "部门修改成功")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        if (UserManager.getAppManager().isLogin()) {
                            UserManager.getAppManager().getmUserInfo().setDepartmentName(value);
                        }
                        finish();
                    }
                }).show().setCanceledOnTouchOutside(false);
    }

}
